import { Prompt, useHistory } from "react-router-dom";
const Com = () => {
  const getHistory = (history) => {
    const location = history.location;
    const user = window.sessionStorage.getItem("user");
    if (location.pathname === "/login" && user === "true") {
      history.replace("/");
    } else if (location.pathname === "/" && user === "false") {
      history.replace("/login");
    }
  };
  getHistory(useHistory());
  
  return (
    <>
      <Prompt
        message={function (location) {
          // location是下一个路由的信息
          const user = window.sessionStorage.getItem("user");
          if (location.pathname === "/login" && user === "true") {
            return false;
          } else if (location.pathname === "/" && user === "false") {
            return false;
          } else {
            return true;
          }
        }}
      />
    </>
  );
};

export default Com;
